<?php
/**
 * 《我的电话》实现类
 *
 * @author 孙晓晔
 * @version $Id: My.class.php 879 2010-01-20 03:29:24Z sunxy $
 */
require_once(dirname(__FILE__) . '/Cdr.class.php');

class My extends Cdr {
	
	public $orderby = array(
		'-' => '请选择',
		'calldate' => '时间',
		'duration' => '时长',
		'dst' => '目的地',
	);
		
	function __construct() {
		$request['update'] = '$Date: 2009/02/16 07:36:07 $';
		$request['revision'] = '$Revision: 1.1 $';

		parent::__construct($request);
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function setting($controller, $request) {
		
		$controller->display($request, 'tel/cdr/my/setting');
		return true;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function find($controller, $request) {
		$this->pretreat($controller);
		
		$my = $_SESSION['emp'];				
		if (empty($my['exten']) || !preg_match('/^(2|6|8)\d{3}/i', $my['exten'])) {
			$controller->assign('msg', "对不起，你的帐号未设置分机，不使用此功能！");
			$controller->display($request, 'info');
			return true;
		} 
			
		$controller->assign('t1', strtotime('-1 day'));
		$controller->assign('t2', strtotime('+1 day'));

		$controller->display($request, 'tel/cdr/my/find');
		return true;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function findPost($controller, $request) {
		$request['my'] = 'my';
		
		$my = $_SESSION['emp'];
				
		if (preg_match('/^6\d{3}/i', $my['exten'])) {
			$request['cdr_s'] = 'ns_sip';
		} else if (preg_match('/^8\d{3}/i', $my['exten'])) {
			$request['cdr_s'] = 'sj_sip';
		}

		return parent::findPost($controller, $request);
	}
	
	/**
	 * 
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function update($controller, $request) {
		$this->pretreat($controller);
		
		$my = $_SESSION['emp'];		
		$exten = $my['exten'];
		if (empty($exten)) {
			$controller->assign("msg", "对不起，您没有分机，不能设置");
			$controller->display($request, 'info');
			return true;
		}
		
		if (preg_match("/^6\d{3}/i", $exten)) {
			$am = astman('ns');
		} else if (preg_match("/^8\d{3}/i", $exten)) {
			$am = astman('sj');
		} else if (preg_match("/^2\d{3}/i", $exten)) {
			$am = astman('fy');
		} else {
			$controller->assign("msg", "对不起，您的分机设置有误，请联系管理员");
			$controller->display($request, 'info');
			return true;
		}
		
		$controller->assign("exten_cf", $am->database_get('CF', $exten));
		$controller->assign("exten_cfu", $am->database_get('CFU', $exten));
		$controller->assign("exten_cfb", $am->database_get('CFB', $exten));
		
		$controller->assign("cw", $am->database_get('CW', $exten));
		$controller->assign("dnd", $am->database_get('DND', $exten));
							
		$controller->display($request, 'tel/cdr/my/cf');
		return true;
	}
	
	/**
	 * 
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function cfPost($controller, $request) {
		$this->pretreat($controller);
		
		$my = $_SESSION['emp'];		
		$exten = $my['exten'];
				
		$exten_cf = $request['exten_cf'];
		$exten_cfu = $request['exten_cfu'];
		$exten_cfb = $request['exten_cfb'];
		
		$cf = $request['cf'];
		$cfu = $request['cfu'];
		$cfb = $request['cfb'];
		
		if (preg_match("/^6/i", $exten)) {
			$am = astman('ns');
		} else if (preg_match("/^8/i", $exten)) {
			$am = astman('sj');
		} else if (preg_match("/^2/i", $exten)) {
			$am = astman('fy');
		}
		
		if (empty($cf) || empty($exten_cf)) {
			$ret = $am->database_del('CF', $exten);
		} else {
			$ret = $am->database_put('CF', $exten, $exten_cf);
		}
		
		/*
		if (empty($cfu) || empty($exten_cfu)) {
			$ret = $am->database_del('CFU', $exten);
		} else {
			$ret = $am->database_put('CFU', $exten, $exten_cfu);
		}*/
		
		if (empty($cfb) || empty($exten_cfb)) {
			$ret = $am->database_del('CFB', $exten);
		} else {
			$ret = $am->database_put('CFB', $exten, $exten_cfb);
		}

		$am->disconnect();
			
		// $this->findPost($controller, $request);
		return true;
	}
	
	/**
	 * 
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function updatePost($controller, $request) {
		$this->pretreat($controller);
		
		$my = $_SESSION['emp'];		
		$exten = $my['exten'];
		
		$cw = $request['cw'];
		$dnd = $request['dnd'];
		
		if (preg_match("/^6/i", $exten)) {
			$am = astman('ns');
		} else if (preg_match("/^8/i", $exten)) {
			$am = astman('sj');
		} else if (preg_match("/^2/i", $exten)) {
			$am = astman('fy');
		}
				
		if (empty($cw)) {
			$ret = $am->database_del('CW', $exten);
		} else {
			$ret = $am->database_put('CW', $exten, 'ENABLED');
		}
		
		if (empty($dnd)) {
			$ret = $am->database_del('DND', $exten);
		} else {
			$ret = $am->database_put('DND', $exten, 'YES');
		}

		$am->disconnect();
			
		// $this->findPost($controller, $request);
		return true;
	}
	
	function t($controller, $request) {

		$am = astman("sj");

		// $am->Originate('SIP/6595', '913760120595', 'tt', '1', NULL, NULL, 8000, '6595', NULL, NULL, NULL, NULL);

		// $ret = $am->Status('SIP/6595');
		// $ret = $am->database_show('CW 6595');
		// $ret = $am->Command('sip show peer 6595');
		$ret = $am->Command('database showkey 8025');
		print_r($ret);

		$am->disconnect();
		logdate($controller, $request);
		return true;
	}
}
?>
